package com.sjb.cp.controller;

import cn.hutool.core.util.ObjectUtil;
import com.sjb.cp.common.BaseResponse;
import com.sjb.cp.common.ResultUtils;
import com.sjb.cp.exception.ErrorCode;
import com.sjb.cp.exception.ThrowUtils;
import com.sjb.cp.manager.auth.annotation.SaSpaceCheckPermission;
import com.sjb.cp.model.constant.SpaceUserPermissionConstant;
import com.sjb.cp.model.po.SpaceUser;
import com.sjb.cp.model.po.User;
import com.sjb.cp.model.request.spaceuser.SpaceUserAddRequest;
import com.sjb.cp.model.request.spaceuser.SpaceUserDeleteRequest;
import com.sjb.cp.model.request.spaceuser.SpaceUserEditRequest;
import com.sjb.cp.model.request.spaceuser.SpaceUserQueryRequest;
import com.sjb.cp.model.vo.SpaceUserVo;
import com.sjb.cp.service.SpaceUserService;
import com.sjb.cp.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author:sjb
 * @CreateTime:2025-02-14
 * @Description: 空间用户控制视图
 * @Version：1.0
 */
@RestController
@RequestMapping("/spaceUser")
public class SpaceUserController {

    @Resource
    private SpaceUserService spaceUserService;

    @Resource
    private UserService userService;

    /**
     * 添加空间成员
     * @param spaceUserAddRequest
     * @return
     */
    @PostMapping("/add")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Long> addSpaceUser(@RequestBody SpaceUserAddRequest spaceUserAddRequest) {
        ThrowUtils.throwIf(spaceUserAddRequest == null, ErrorCode.PARAMS_ERROR);
        long addSpaceUser = spaceUserService.addSpaceUser(spaceUserAddRequest);
        return ResultUtils.success(addSpaceUser);
    }

    @PostMapping("/remove")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Boolean> removeSpaceUser(@RequestBody SpaceUserDeleteRequest spaceUserDeleteRequest){
        ThrowUtils.throwIf(spaceUserDeleteRequest == null, ErrorCode.PARAMS_ERROR);
        Long id = spaceUserDeleteRequest.getId();
        SpaceUser spaceUser = spaceUserService.getById(id);
        ThrowUtils.throwIf(spaceUser == null, ErrorCode.NOT_FOUND_ERROR);

        boolean removed = spaceUserService.removeById(id);
        return ResultUtils.success(removed);
    }

    /**
     * 查询空间用户某个成员信息
     * @param spaceUserQueryRequest
     * @return
     */
    @PostMapping("/getSpaceUserInfo")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<SpaceUser> getSpaceUserInfo(@RequestBody SpaceUserQueryRequest spaceUserQueryRequest){
        ThrowUtils.throwIf(spaceUserQueryRequest == null, ErrorCode.PARAMS_ERROR);
        Long userId = spaceUserQueryRequest.getUserId();
        Long spaceId = spaceUserQueryRequest.getSpaceId();
        ThrowUtils.throwIf(ObjectUtil.hasEmpty(userId, spaceId), ErrorCode.PARAMS_ERROR);

        SpaceUser spaceUser = spaceUserService.getOne(spaceUserService.getQueryWrapper(spaceUserQueryRequest));
        return ResultUtils.success(spaceUser);
    }

    @PostMapping("/list")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<List<SpaceUserVo>> getSpaceUserVoList(@RequestBody SpaceUserQueryRequest spaceUserQueryRequest){
        ThrowUtils.throwIf(spaceUserQueryRequest == null, ErrorCode.PARAMS_ERROR);

        List<SpaceUser> spaceUserList = spaceUserService.list(spaceUserService.getQueryWrapper(spaceUserQueryRequest));
        List<SpaceUserVo> spaceUserVoList = spaceUserService.getSpaceUserVoList(spaceUserList);
        return ResultUtils.success(spaceUserVoList);
    }

    @PostMapping("/edit")
    @SaSpaceCheckPermission(value = SpaceUserPermissionConstant.SPACE_USER_MANAGE)
    public BaseResponse<Boolean> editSpaceUser(@RequestBody SpaceUserEditRequest spaceUserEditRequest){
        ThrowUtils.throwIf(spaceUserEditRequest == null,ErrorCode.PARAMS_ERROR);

        Long id = spaceUserEditRequest.getId();
        SpaceUser spaceUser = new SpaceUser();
        BeanUtils.copyProperties(spaceUserEditRequest, spaceUser);
        SpaceUser oldSpaceUser = spaceUserService.getById(id);
        ThrowUtils.throwIf(oldSpaceUser == null, ErrorCode.NOT_FOUND_ERROR);
        boolean updated = spaceUserService.updateById(spaceUser);
        return ResultUtils.success(updated);
    }

    @PostMapping("/list/my")
    public BaseResponse<List<SpaceUserVo>> getMySpaceUser(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        SpaceUserQueryRequest spaceUserQueryRequest = new SpaceUserQueryRequest();
        spaceUserQueryRequest.setUserId(userId);
        List<SpaceUser> spaceUserList = spaceUserService.list(spaceUserService.getQueryWrapper(spaceUserQueryRequest));
        List<SpaceUserVo> spaceUserVoList = spaceUserService.getSpaceUserVoList(spaceUserList);
        return ResultUtils.success(spaceUserVoList);

    }

}
